package com.younion.action.consumer.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IRegisterUserService;
import com.younion.model.ModifyPsdResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.MD5;
import com.younion.vo.RegisterUserVO;

public class ModifyPsdConsumer extends AbstractRequestConsumer {
	private IRegisterUserService registerUserService = null;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return modifyPsd(request, response);
	}

	public String modifyPsd(HttpServletRequest request,
			HttpServletResponse response) {
		ModifyPsdResult modifyPsdResult = new ModifyPsdResult();
		String userToken = request.getParameter("user_token");
		String oldPassword = request.getParameter("old_password");
		String newPassword = request.getParameter("new_password");
		Map paramMap = new HashMap();
		paramMap.put("userToken", userToken);
		paramMap.put("password", oldPassword);
		ResultObject resultObject = new ResultObject();
		try {
			RegisterUserVO registerUserVO = registerUserService
					.getVOByMap(paramMap);
			if (registerUserVO == null) {// 旧密码有错误
				resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
				modifyPsdResult.setSuccessful("0");
				resultObject.setResult(modifyPsdResult);
				resultObject.setMsg("旧密码输入错误");
			} else {// 进行密码更新
				registerUserVO.setPassword(newPassword);
				registerUserService.save(registerUserVO);
				modifyPsdResult.setSuccessful("1");
				resultObject.setResult(modifyPsdResult);
				resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
			}
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			modifyPsdResult.setSuccessful("0");
			resultObject.setResult(modifyPsdResult);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setRegisterUserService(IRegisterUserService registerUserService) {
		this.registerUserService = registerUserService;
	}
}
